import { userLoginAPI, getUserInfoAPI } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: null, // 用户token
    userInfo: null // 用户信息
  },
  mutations: {
    // 储存用户token
    setToken(state, token) {
      state.token = token
    },
    // 删除token
    removeToken(state) {
      state.token = null
    },
    // 储存用户信息
    setUserInfo(state, info) {
      state.userInfo = info
    }
  },
  actions: {
    async loginAction({ commit }, date) {
      // 获取token
      const { data } = await userLoginAPI(date)
      commit('setToken', data.token)
      // 获取个人信息
      const res = await getUserInfoAPI(data.userId)
      commit('setUserInfo', res.data)
    }
  }
}
